package com.lin.reggie.mapper;

import com.lin.reggie.dto.DishDto;
import com.lin.reggie.entity.Dish;
import com.lin.reggie.entity.DishFlavor;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface DishMapper {
    //根据类别id查询菜品的数量
    @Select("select count(*) from dish where category_id = #{cid}")
    Integer findByCid(long cid);

    //插入菜品数据
    @Insert("insert into  dish values(null,#{name},#{categoryId},#{price},#{code},#{image},#{description}," +
            "#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    //默认情况：数据库表中这条记录有主键，但是dishDto的属性id的为null
    //目标：执行插入后将数据库生成的id数据返回设置给dishDto对象中id属性
    //解决方案：@Options，
    //useGeneratedKeys = true 获取生成的主键
    //keyColumn = "id",keyProperty = "id" 将数据库表中生成的id值赋值给dishDto对象的属性id
    @Options(useGeneratedKeys = true, keyColumn = "id" ,keyProperty = "id")
    Integer insert(DishDto dishDto);

    List<DishDto>findAll2(@Param("name") String name);

    //回显修改
    Dish findById(Long id);

    //修改

    void updateDish(DishDto dishDto);

    List<DishFlavor>findByCategoryId(Long categoryId);

    List<Dish>findByCategoryIdAndStatus(@Param("categoryId") Long categoryId, @Param("status") Integer status);
}
